package com.oplink.webems.modules.alarm.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.oplink.webems.common.persistence.Page;
import com.oplink.webems.modules.alarm.entity.AlarmProp;
import com.oplink.webems.modules.alarm.entity.AlarmSeverity;
import com.oplink.webems.modules.alarm.entity.AlarmType;
import com.oplink.webems.modules.alarm.service.AlarmPropService;
import com.oplink.webems.modules.cfg.entity.CfgCardType;
import com.oplink.webems.modules.cfg.entity.CfgNe;
import com.oplink.webems.modules.cfg.entity.CfgSlot;
import com.oplink.webems.modules.cfg.services.CfgCardTypeService;
import com.oplink.webems.modules.cfg.services.CfgNeService;
import com.oplink.webems.modules.cfg.services.CfgNetypeService;
import com.oplink.webems.modules.cfg.services.CfgSeverityService;
import com.oplink.webems.modules.cfg.services.CfgSiteService;
import com.oplink.webems.modules.cfg.services.CfgSlotService;
import com.oplink.webems.modules.sys.entity.User;
import com.oplink.webems.modules.sys.service.ShiroDbRealm.ShiroUser;
import com.oplink.webems.modules.sys.web.BaseController;

/**
 * Alarm Properties Controller
 * @author yizhang
 */
@Controller
@RequestMapping(value = "alarm/alarmprop")
public class AlarmPropController extends BaseController {
	
	@Autowired
	private AlarmPropService alarmPropService;
	
	@Autowired
	private CfgSiteService siteService;
	
	@Autowired
	private CfgNeService neService;
	
	@Autowired
	private CfgSeverityService severityService;
	
	@Autowired
	private CfgNetypeService netypeService;
	
	@Autowired
	private CfgSlotService slotService;
	
	@Autowired
	private CfgCardTypeService CfgCardService;
	
	@ModelAttribute
	public AlarmProp get(@RequestParam(required = false) Long id) {
		if (id != null) {
			return alarmPropService.getAlarmprop(id);
		} else {
			return new AlarmProp();
		}
	}	
	
	@RequestMapping(value = { "list", "" })
	public String list(AlarmProp alarmprop, HttpServletRequest request, HttpServletResponse response, Model model) {
//		List<CfgSite> slist = siteService.getAllSite();
//		List<CfgNe> nelist = neService.getAllNe();
		List<AlarmSeverity> selist = severityService.getAllSeverity();
		List<AlarmType> atlist = alarmPropService.getAllAlarmType();
		
		List<CfgCardType> ctypelist = CfgCardService.getAllCardType();
		
//		List<CfgNeType> netypelist = netypeService.getAllNeType();
//		List<CfgSlot> slotlist = slotService.getAllSlot();
		//Long userId = getCurrentUserId();
		Page<AlarmProp> pages = alarmPropService.find(new Page<AlarmProp>(request, response), alarmprop);
		model.addAttribute("pages", pages);
//		model.addAttribute("slist", slist);
//		model.addAttribute("nelist", nelist);
		model.addAttribute("selist", selist);
//		model.addAttribute("slotlist", slotlist);
		model.addAttribute("atlist", atlist);
//		model.addAttribute("netypelist", netypelist);
		model.addAttribute("ctypelist", ctypelist);
		model.addAttribute("aprop", alarmprop);
		return "modules/alarm/alarm_prop";
	}	

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	@ResponseBody
	public String oneAlarmprop(@PathVariable("id") Long id) {
        AlarmProp alarmprop = alarmPropService.getAlarmprop(id);
        String json = alarmPropService.getAlarmPropJson(alarmprop);
		return json;
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String frmData = request.getParameter("jstr");
		try {
			User user = new User(getCurrentUserId());
			AlarmProp aprop = alarmPropService.getAlarmPropData(frmData);
			alarmPropService.saveAlarmProp(aprop);
			redirectAttributes.addFlashAttribute("message", "save successful!");
			return "1";
		} catch (Exception e) {
			return "0";
		}
	}	
	
	@ResponseBody
	@RequestMapping(value = "getNe")
	public String getNe(String sid) {
		List<CfgNe> nelist = siteService.getNeBySite(sid);
		//List<CfgSite> slist = siteService.getAllSite();
		String neJson = JSONObject.toJSONString(nelist);
		return neJson;
	}	
	
	@ResponseBody
	@RequestMapping(value = "getUseCardSlot")
	public String getUseCardSlot(String neid) {
		List<Map> slotlist = slotService.getUseCardSlot(neid);
		//List<CfgSite> slist = siteService.getAllSite();
		String slotJson = JSONObject.toJSONString(slotlist);
		return slotJson;
	}	
	
	@ResponseBody
	@RequestMapping(value = "getSlot")
	public String getSlotByNe(String neid) {
		CfgNe neobj = neService.getOneCfgNe(Long.parseLong(neid));
		List<CfgSlot> slist = neobj.getSlotlist();
		List<Map> cardlist = alarmPropService.getCardInfo(slist);
		String slotJson = JSONObject.toJSONString(cardlist);
		return slotJson;
	}	
	
	/**
	 * 取出Shiro中的当前用户Id.
	 */
	private Long getCurrentUserId() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user.id;
	}	
}
